﻿#include "ApMainWindow.h"
#include <AsConfig>

//#define ONCE_DATA_LENGTH 1000

ApMainWindow::ApMainWindow(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::ApMainWindowClass())
{
	ui->setupUi(this);
	init();
	initSonDialog();
	initCustomPlot();

	connect(m_pCore, &ProcessCore::sendLatestPCIDatas, this, &ApMainWindow::flushCustomPlot);
	connect(m_pCore, &ProcessCore::shouleReleaseButtons, this, &ApMainWindow::releaseButtons);
	connect(m_pCore, &ProcessCore::extrastGirdleOver, this, &ApMainWindow::aviliable3DButton);
	connect(m_pCore, &ProcessCore::update3DWidgetSlider, m_show3dDialog, &Refactor3DDialog::updateSlider);
	connect(m_communicationDialog, &CommunicationDialog::sendPortInfo, m_pCore, &ProcessCore::updatePortInformation);
	connect(ui->listWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(on_context_menu(QPoint)));
	connect(ui->listWidget, &QListWidget::currentItemChanged, this, &ApMainWindow::OnSelectListWidgetItem);
	
}

ApMainWindow::~ApMainWindow()
{
	delete m_pCore;
	delete m_pThread;
	delete m_exportor;
	delete ui;
}

void ApMainWindow::init()
{
	m_pCore = new ProcessCore();
	m_summaryItem = ui->listWidget->currentItem();
	m_pThread = new AsThread(this);
	m_exportor = new DataExportor(this);
	//ui->pushButton_show3d->setEnabled(false);
	ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
}

void ApMainWindow::initCustomPlot()
{
	// 设置图表样式
	ui->customPlot->xAxis->setLabel("DataNumber");
	ui->customPlot->yAxis->setLabel("Value");
	ui->customPlot->xAxis->setRange(0, 10);
	ui->customPlot->yAxis->setRange(-2, 2);

	//设置标题
	QCPTextElement *title = new QCPTextElement(ui->customPlot, "实时信号显示", QFont("sans", 14, QFont::Bold));
	ui->customPlot->plotLayout()->insertRow(0);
	ui->customPlot->plotLayout()->addElement(0, 0, title);
}

void ApMainWindow::initSonDialog()
{
	m_configDialog = new ConfigDialog(this);
	m_communicationDialog = new CommunicationDialog(this);
	m_show3dDialog = new Refactor3DDialog(this);
	m_pCore->initCommunicatorFields(m_communicationDialog->getPortInfo());
}

void ApMainWindow::on_pushButton_setting_clicked()
{
	m_configDialog->exec();
}

void ApMainWindow::on_pushButton_debug_clicked()
{
	m_communicationDialog->exec();
}

void ApMainWindow::on_pushButton_show3d_clicked()
{
	m_show3dDialog->exec();	
}

void ApMainWindow::on_pushButton_start_clicked()
{
	ui->pushButton_start->setEnabled(false);
	ui->label_showInfo->setText("正在检查聚焦高度......");
	qDebug() << "正在检查聚焦高度......";
	if (ui->listWidget->count() <= 1)
	{
		QMessageBox::warning(this, "Warning", "请选择正确的聚焦高度再进行采集");
		ui->pushButton_start->setEnabled(true);
		return;
	}
	if (ui->listWidget->currentItem() == m_summaryItem)
	{
		qDebug() << "当前选择为总览";
		QMessageBox::warning(this, "Tips", "请添加聚焦高度");
		//
	}
	ui->pushButton_confirmHeight->setEnabled(false);
	ui->listWidget->setEnabled(false);
	m_pCore->startWork();
}

void ApMainWindow::on_pushButton_stop_clicked()
{
	m_pCore->stopWork();
	ui->pushButton_start->setEnabled(true);
	ui->listWidget->setEnabled(true);
	ui->pushButton_confirmHeight->setEnabled(true);
}

void ApMainWindow::on_pushButton_pause_clicked()
{
	m_pCore->pauseWork();
}

void ApMainWindow::on_pushButton_continue_clicked()
{
	m_pCore->continueWork();
}

void ApMainWindow::on_pushButton_confirmHeight_clicked()
{
	ui->pushButton_show3d->setEnabled(false);
	ui->lineEdit->setEnabled(false);
	int focusHeight = ui->lineEdit->text().toInt();
	QListWidgetItem *listwidgetItem = new QListWidgetItem("z=" + ui->lineEdit->text());
	if (!PtrCoreData->getListItemMap().contains(focusHeight))
	{
		PtrCoreData->setMapListItem(focusHeight, listwidgetItem);
		int count = ui->listWidget->count();
		ui->listWidget->insertItem(count, listwidgetItem);
	}
	ui->lineEdit->setEnabled(true);
}

void ApMainWindow::on_pushButton_confirmAll_clicked()
{
	if (PtrCoreData->getListItemMap().size() != PtrCoreData->getMatDatasMap().size())
	{
		qDebug() << "数据不完整，请检查";
		return;
	}
	m_pCore->prepareProcess3DDatas();
	//m_pCore->extrastGirdleLevel();
	//ui->pushButton_show3d->setEnabled(true);
}

void ApMainWindow::OnSelectListWidgetItem(QListWidgetItem* item)
{
	qDebug() << item->text();
	qDebug() << ui->listWidget->currentRow();
	/*m_pThread->invokeFn([this,item]() {*/
		ui->customPlot->clearGraphs();
		ui->customPlot->replot();
		int height = PtrCoreData->getHeightWithListItem(item);
		PtrCoreData->setCurrentHeight(height);
		if (!PtrCoreData->getMatDatasMap().contains(height))
		{
			ui->pushButton_start->setEnabled(true);
			return;
		}
		//QVector<double> ydata = PtrCoreData->getOnceAllDataWithHeight(height);
		ui->pushButton_start->setEnabled(false);
		ui->customPlot->xAxis->setRange(0, PtrCoreData->getOnceAllXDataWithHeight(height).size());
		ui->customPlot->addGraph()->setData(PtrCoreData->getOnceAllXDataWithHeight(height), PtrCoreData->getOnceAllYDataWithHeight(height));
		ui->customPlot->replot();
		/*});*/
}

void ApMainWindow::flushCustomPlot(int index)
{
	qDebug() << "flush start" << index;
	ui->customPlot->clearGraphs();
	ui->customPlot->replot();
	ui->label_showInfo->setText("正在加载数据......");
	qDebug() << "setData start" << index;
	ui->customPlot->addGraph()->setData(PtrCoreData->getConstXData(), PtrCoreData->getVecYDatasWithIndex(index));
	ui->customPlot->xAxis->setRange(0, ONCE_DATA_LENGTH);
	// 重新绘制图表
	ui->customPlot->replot();
	qDebug() << "flush over" << index;
}

void ApMainWindow::updatePortInformation(HSS::PortInformation portinfo)
{

}

void ApMainWindow::releaseButtons()
{
	ui->pushButton_start->setEnabled(true);
	ui->pushButton_confirmHeight->setEnabled(true);
	ui->listWidget->setEnabled(true);
}

void ApMainWindow::aviliable3DButton()
{
	ui->pushButton_show3d->setEnabled(true);
}

void ApMainWindow::on_pushButton_export_clicked()
{
	m_pCore->exportToExcel();
}

void ApMainWindow::on_context_menu(const QPoint &point)
{
	QListWidgetItem *item = ui->listWidget->itemAt(point);
	if (item) {
		QMenu menu(this);
		QAction *delete_action = new QAction("删除", this);
		connect(delete_action, &QAction::triggered, [item, this]() {
			if (item == m_summaryItem)
			{
				QMessageBox::warning(this, "WARNING", "请勿删除此项！！");
				return;
			}
			ui->listWidget->takeItem(ui->listWidget->row(item));
			int height = PtrCoreData->getListItemMap().key(item);
			PtrCoreData->getListItemMap().remove(height);

			delete item;
			});
		menu.addAction(delete_action);
		menu.exec(ui->listWidget->mapToGlobal(point));
	}
}

void ApMainWindow::pciParamChanged()
{
	m_pCore->loadProfile(PtrConfig->xmlPath);
	qDebug() << "PCIXML has changed...";
}

